package com.job.meeting.pojo;

import lombok.Data;
import org.springframework.http.codec.xml.Jaxb2XmlEncoder;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

@Data
public class sendMessage {
    private String toUserName;
    private String fromUserName;
    private int createTime;
    private String msgType;
    private String content;

    //构造函数，接受xml字段来构造对应对象
    public sendMessage(String xml) throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilderFactory d = DocumentBuilderFactory.newDefaultInstance();
        DocumentBuilder builder = d.newDocumentBuilder();
        InputSource inputSource = new InputSource(new StringReader(xml));
        Document document = builder.parse(inputSource);
        setToUserName(document.getElementsByTagName("toUserName").item(0).getNodeValue());
        setFromUserName(document.getElementsByTagName("fromUserName").item(0).getNodeValue());
        setCreateTime(Integer.parseInt(document.getElementsByTagName("createTime").item(0).getNodeValue()));
        setMsgType(document.getElementsByTagName("msgType").item(0).getNodeValue());
        setContent(document.getElementsByTagName("content").item(0).getNodeValue());
    }

    //覆写toString函数，获得对象的xml表达
    /*public String toString(){
        StringWriter stringWriter = new StringWriter();
    }*/
/*
    public String getToUserName() {
        return toUserName;
    }

    public void setToUserName(String toUserName) {
        this.toUserName = toUserName;
    }

    public String getFromUserName() {
        return fromUserName;
    }

    public void setFromUserName(String fromUserName) {
        this.fromUserName = fromUserName;
    }

    public int getCreateTime() {
        return createTime;
    }

    public void setCreateTime(int createTime) {
        this.createTime = createTime;
    }

    public String getMsgType() {
        return msgType;
    }

    public void setMsgType(String msgType) {
        this.msgType = msgType;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

 */
}
